#include "dataview.h"
#include "ui_dataview.h"
#include "eventcenter.h"
#include "UIEditor/uilib.h"
#include "UIEditor/mylabel.h"
#include "UIEditor/uilib.h"
#include "GameData/jsontranslator.h"
#include "ResourceManger/filemanager.h"
#include <QAbstractItemView>
DataView::DataView(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::DataView)
{
    Init();
    RefreshDataItem();
}

DataView::~DataView()
{
    delete ui;
}

void DataView::AddDataItem(QString name, int i)
{
    AddDataItemWidget(name,QString::number(i));
}

void DataView::AddDataItem(QString name, float f)
{
    AddDataItemWidget(name,QString::number(f));
}

void DataView::AddDataItem(QString name, bool b)
{
    QString value = b?"是":"否";
    AddDataItemWidget(name,value);
}

void DataView::AddDataItem(QString name, QString s)
{
    AddDataItemWidget(name,s);
}

void DataView::AddDataItemWidget(QString name, QString value)
{
    float size = 150.0f;
    MyLabel* myLabel = new MyLabel();
    myLabel->SetSelectedDisabled(true);
    myLabel->setText(name + "：" + value);
    myLabel->Refresh(UILib::Instance().labels["数值-数值框"]);
    //自动转行
    myLabel->setWordWrap(true);
    QListWidgetItem* item = new QListWidgetItem();

    item->setSizeHint(QSize(size,size));
    ui->dataList->addItem(item);
    ui->dataList->setItemWidget(item,myLabel);
    //不可选
    ui->dataList->setSelectionMode(QAbstractItemView::NoSelection);
}

void DataView::Init()
{
    ui->setupUi(this);
    QString qss0 = "QListWidget{border-image:url(\"";
    QString qss1 = "\");padding-left: 20px;padding-right: 20px;padding-top:30px;padding-bottom:30px;}";
    if(UILib::Instance().labels["数值-容器"])
    {
        ui->dataList->setStyleSheet(qss0 + UILib::Instance().labels["数值-容器"]->img + qss1);
    }
    if(UILib::Instance().labels["数值-数值框"])
    {
        img = UILib::Instance().labels["数值-数值框"]->img;
    }
    connect(&EventCenter::Instance(),&EventCenter::UIChange,this,[=](QString name){
        if(name == "数值-容器" && UILib::Instance().labels[name])
        {
            ui->dataList->setStyleSheet(qss0 + UILib::Instance().labels[name]->img + qss1);
        }
        if(name == "数值-数值框" && UILib::Instance().labels[name])
        {
            img = UILib::Instance().labels[name]->img;
        }
    });
    if(UILib::Instance().btns.contains("返回游戏") && UILib::Instance().btns["返回游戏"])
        ui->BackBtn->info = UILib::Instance().btns["返回游戏"];
    connect(&EventCenter::Instance(),&EventCenter::RefreshData,[=](){
        RefreshDataItem();
    });
}

void DataView::RefreshDataItem()
{
    if(GameData::Instance().visible.count() <= 0)
    {
        QJsonObject* ob = FileManager::Instance().ReadJsonFile(FileManager::Instance().gameDataPath);
        JsonTranslator jt;
        jt.ReadData(ob);
    }
    ui->dataList->clear();
    for(auto it = GameData::Instance().stringData.begin();it != GameData::Instance().stringData.end();it++)
    {
        if(GameData::Instance().visible[it.key()])
        {
            AddDataItem(it.key(),it.value());
        }
    }
    for(auto it = GameData::Instance().intData.begin();it != GameData::Instance().intData.end();it++)
    {
        if(GameData::Instance().visible[it.key()])
        {
            AddDataItem(it.key(),it.value());
        }
    }
    for(auto it = GameData::Instance().floatData.begin();it != GameData::Instance().floatData.end();it++)
    {
        if(GameData::Instance().visible[it.key()])
        {
            AddDataItem(it.key(),it.value());
        }
    }
    for(auto it = GameData::Instance().boolData.begin();it != GameData::Instance().boolData.end();it++)
    {
        if(GameData::Instance().visible[it.key()])
        {
            AddDataItem(it.key(),it.value());
        }
    }
}
